package com.xzk.utillibary.comman;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by xzk on 2018/3/19 0019.
 * 时间处理类
 * 将时间戳转为固定格式的时间进行显示
 * 1,问题 今天 昨天  前天   年月日时分秒
 */

public class TimeUtil {

    public static String yymmhhss = "yyyy-MM-dd HH:mm:ss";
    public static String hhss = "HH:mm:ss";
    public static String yymmdd = "yyyy-MM-dd";
    private static SimpleDateFormat sdfHHSS = new SimpleDateFormat("HH:mm:ss");
    private static SimpleDateFormat sdfyymmHHSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    private static String today = "今天";
    private static String yestoday = "昨天";
    private static String beforeYseday = "前天";

    /**
     * 计算时间戳差值
     *
     * @param targetSeconds
     * @return
     */
    public static String getTimeFromLong(Long targetSeconds) {
        Calendar aCalender = Calendar.getInstance();
        aCalender.setTimeInMillis(targetSeconds);//小的
        int day1 = aCalender.get(Calendar.DAY_OF_YEAR);
        aCalender.setTimeInMillis(System.currentTimeMillis());
        int day2 = aCalender.get(Calendar.DAY_OF_YEAR);
        int day3 = day2 - day1;
        if(day3<0)
            return "超过系统时间："+sdfyymmHHSS.format(targetSeconds);
        if (day3 < 1) {
            return today + " " + sdfHHSS.format(targetSeconds);
        } else if (day3 < 2) {
            return yestoday + " " + sdfHHSS.format(targetSeconds);
        } else if (day3 < 3) {
            return beforeYseday + " " + sdfHHSS.format(targetSeconds);
        } else {
            return sdfyymmHHSS.format(targetSeconds);
        }
    }

    public static String formatTime(long mills, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date(mills);
        return formatTime(sdf, date);
    }

    /**
     * 格式化对应结构字符串
     *
     * @param sdf
     * @param date
     * @return
     */
    private static String formatTime(SimpleDateFormat sdf, Date date) {
        String format = sdf.format(date);
        return format;
    }

    public static long currentMillis() {
        return System.currentTimeMillis();
    }

    /**
     * 格式化毫秒显示
     *
     * @return
     */
    public static String formatMillis(long millis) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss-SSS").format(millis);
    }

    /**
     * 获取当前微秒时间， long[0] 毫秒   long[1] 微秒
     *
     * @return
     */
    public static long[] currentMicroscoend() {
        return currentMicroscoendC();
    }

    static {
        System.loadLibrary("com_xzk_utillibary_comman_TimeUtil");
    }

    public static native long[] currentMicroscoendC();

}
